package yl.hs.bmipfull.viewmodels.system;

import yl.hs.bmipfull.security.Aes;
import yl.hs.bmipfull.utils.AESUtil;
import yl.hs.bmipfull.utils.CK;
import yl.hs.bmipfull.utils.MachineUtils;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActiveDeviceType;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;

/*
 * BlueAreaData
 * @category
 * @author 谢景广
 * @date 2022-05-11
 */
public class BlueAreaData {
    private String randomCode =Tl.getRandomCode(10);
    private String activeSN;
    private String machineCode = MachineUtils.makeMachineCode();;
    private String endTime;
    private String activeTime = Tl.getDateString(new Date(),"yyyy-MM-dd HH:mm:ss");;
    private String state  ="正常";
    public String getRandomCode() {
        return randomCode;
    }

    public void setRandomCode(String randomCode) {
        this.randomCode = randomCode;
    }

    public String getActiveSN() {
        return activeSN;
    }

    public void setActiveSN(String activeSN) {
        this.activeSN = activeSN;
    }

    public String getMachineCode() {
        return machineCode;
    }

    public void setMachineCode(String machineCode) {
        this.machineCode = machineCode;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getActiveTime() {
        return activeTime;
    }

    public void setActiveTime(String activeTime) {
        this.activeTime = activeTime;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public BlueAreaData(){
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE,3);
        Calendar newCal = Calendar.getInstance();
        newCal.setTime(new Date());
        newCal.add(Calendar.DATE,-3);
        this.endTime = Tl.getDateString(newCal.getTime(),"yyyy-MM-dd");
        this.activeSN = cal.getTime().getTime()+"";
    }
    public BlueAreaData(ActiveDeviceType type){
        this.machineCode = MachineUtils.makeMachineCode(type);
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        cal.add(Calendar.DATE,3);
        Calendar newCal = Calendar.getInstance();
        var startTime = new Date();
        newCal.setTime(startTime);
        newCal.add(Calendar.DATE,3);
        this.endTime = Tl.getDateString(newCal.getTime(),"yyyy-MM-dd");
        this.activeSN = cal.getTime().getTime()+"";
    }

    public boolean validActiveSN(String activeSN){
       var tv = getSNValue(this.activeSN);
       var cv = getSNValue(activeSN);
       return tv>= cv;
    }

    private Long getSNValue(String s){
       var st = s.replaceAll("[A-Za-z]+", "");
        return Long.parseLong(st);
    }

    public static BlueAreaData parse(byte[] data) throws Exception {
       var nd = AESUtil.encryptData(data, CK.ByteEncryptKey);
        var st = new String(nd,StandardCharsets.UTF_8);
       return parse(st);
    }

    public byte[] toStream() throws Exception {
        var json = Tl.toJson(this);
        json = Aes.encrypt(json);
        return AESUtil.encryptData(json.getBytes(StandardCharsets.UTF_8), CK.ByteEncryptKey);
    }

    public static BlueAreaData parse(String data) throws Exception {
       var json = Aes.decrypt(data);
       return Tl.toObject(json,BlueAreaData.class);
    }
}
